#!/usr/bin/python3
import pymysql

from extend.model import Img


host = '101.34.250.170'
port = 3306
user = 'root'
password = 'asdf12345'
database = 'bing'


def save_img(img: Img):
    # 创建连接
    conn = pymysql.connect(host=host, port=port, user=user, password=password, database=database)

    # 创建游标对象
    cursor = conn.cursor()

    sql = f"insert into img(date, title, copyright, h_img, v_img) values ('{img.date}', '{img.title}', '{img.copyright}', '{img.h_img}', '{img.v_img}')"
    try:
        # 执行 SQL
        cursor.execute(sql)
        # 提交更改
        conn.commit()
    finally:
        # 关闭游标和连接
        cursor.close()
        conn.close()

